package com.example.book.controller;

import com.example.book.constant.Constants;
import com.example.book.model.*;
import com.example.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.plaf.SplitPaneUI;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/getBookList")
    public Result getBookList(PageRequest pageRequest, HttpSession session) {
        log.info("查询翻页信息,pageRequest: {}",pageRequest);
//        // 校验用户是否登录
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if (userInfo == null || userInfo.getId() <= 0 || "".equals(userInfo.getUserName())) {
//            // 用户未登录
//            return Result.unlogin();
//        }

        if(pageRequest.getPageSize() < 0 || pageRequest.getCurrentPage() < 1) {
            return Result.fail("参数校验失败");
        }
        PageResult<BookInfo> bookList = null;
        try {
            bookList = bookService.getBookList(pageRequest);
            return Result.success(bookList);
        }catch (Exception e) {
            log.info("查询翻页信息错误,e: {}",e);
            return Result.fail("查询失败,请联系管理员");
        }
    }

    /**
     * 根据id 查询图书
     * @param bookId 图书id
     * @return 返回图书对象
     */
    @RequestMapping("/queryBookInfoById")
    public BookInfo queryBookInfoById(Integer bookId) {
        log.info("接受到的id: {}",bookId);
        if(bookId == null || bookId <= 0) {
            return null;
        }
        BookInfo bookInfo = null;
        try {
            bookInfo = bookService.queryBookInfoById(bookId);
        }catch (Exception e) {
            log.error("获取bookInfo异常: {}",e);
        }
        return bookInfo;
    }

    /**
     * 修改图书,
     * @param bookInfo 需要修改的图书对象
     * @return 修改成功返回 "" , 修改失败返回错误信息
     */
    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo) {
        log.info("接受到的修改数据: {}",bookInfo);
        if(bookInfo == null) {
            return "修改错误,请联系管理员";
        }
        Integer reslut = bookService.updateBook(bookInfo);
        if(reslut == 0) {
            return "修改图书失败,请联系管理员";
        }
        return "";
    }

    /**
     * 添加图书
     * @param bookInfo 图书对象
     * @return
     */
    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo) {
        log.info("添加图书接受请求bookInfo:{}",bookInfo);
        if (!StringUtils.hasLength(bookInfo.getBookName())
            || !StringUtils.hasLength(bookInfo.getAuthor())
            || bookInfo.getCount() == null
            || bookInfo.getCount() < 0
            || bookInfo.getPrice() == null
            || !StringUtils.hasLength(bookInfo.getPublish())
            || bookInfo.getStatus() < 0) {

            return "添加图书失败,请联系管理员";
        }
        Integer integer = bookService.addBook(bookInfo);
        if(integer <= 0) {
            return "添加图书失败,请联系管理员";
        }
        return "";
    }
    /**
     * 批量删除图书(逻辑删除)
     */
    @ResponseBody
    @RequestMapping(value = "/batchDelete",produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids) {
        log.info("批量删除请求 ids:{}",ids);
        if(ids == null || ids.size() <= 0) {
            return "批量删除失败,请联系管理员";
        }
        Integer integer = bookService.batchDelete(ids);
        if(integer <= 0) {
            log.error("批量删除图书失败 ids:{}",ids);
            return "批量删除失败,请联系管理员";
        }
        return "";
    }
}
